package com.BluemSun.Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet
public class registerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("newUsername");
        String password = request.getParameter("newPassword1");
        String s = request.getParameter("newPassword2");
        if(username=="" || password==""){
            response.sendRedirect(request.getContextPath()+"/failRegister.jsp");
        }
        else if(!password.equals(s)){
            response.sendRedirect(request.getContextPath()+"/failRegister3.jsp");
        }
        else if(username.length()<4 || username.length()>16 || password.length()<6 || password.length()>18){
            response.sendRedirect(request.getContextPath()+"/failRegister2.jsp");
        }
        else{
            HttpSession session = request.getSession();
            session.setAttribute("user",username);
            session.setAttribute("pswd",password);
            response.sendRedirect(request.getContextPath()+"/successRegister.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
